package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.SystemInform;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.SystemInformMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ISystemInformService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SystemInformServiceImpl implements ISystemInformService {
    @Autowired
    private SystemInformMapper systemInformMapper;
    @Autowired
    private UserMapper userMapper ;
    @Override
    public void save(SystemInform systemInform) {

        //先新增系统消息，拿到系统消息的id
        systemInformMapper.insert(systemInform);

        //在后台新增系统消息的时候，给当前所有的用户关联起来
        //从数据库中查出所有的用户
        List<User> users = userMapper.selectForList();
        for (User user : users) {
            //循环遍历，拿出每个用户的id，往中间表添加数据
            systemInformMapper.insertMessagesInUser(user.getId(),systemInform.getId());
            //往中间表添加数据
        }
    }

    @Override
    public PageInfo<?> selectForList(QueryObject qo) {
        //分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<SystemInform> list = systemInformMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<SystemInform> getList() {
        return systemInformMapper.selectAll();
    }

    @Override
    public void deleteById(Long userId,Long messageId) {
        systemInformMapper.deleteRelationMessage(userId,messageId);
    }
}
